#include "ImageAxis.h"

#include <QColor>
#include <QFont>
#include <QFontMetrics>
#include <QMap>
#include <QPainter>

using namespace solar;

ImageAxis::ImageAxis(QQuickItem* parent) : QQuickPaintedItem(parent) { this->update(); }

void ImageAxis::paint(QPainter* painter)
{
    // qDebug() <<"image axis painting!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
    if (painter == nullptr)
    {
        return;
    }
    if (!visible_)
    {
        qDebug() << "---- visible = " << visible_;
        return;
    }

    // QRect rect(0, 0, 20, 20);
    // painter->fillRect(rect, QColor("red"));

    if (horizontal_)
    {
        paintHorizontalAxis(painter);
    }
    else
    {
        paintVerticalAxis(painter);
    }
    // QRect ytick_rect(-1 + x_offset_,0+ y_offset_,1,img_height_ );
}
void ImageAxis::paintHorizontalAxis(QPainter* painter)
{

    qDebug() << "horizontal axis: height=" << height() << " width=" << width()
             << " axis_length_=" << axis_length_;

    int axis_height = 1;
    QRect tick_rect(offset_, 0, axis_length_, axis_height);
    painter->fillRect(tick_rect, QColor("red"));
    for (int i = 0; i < tick_num_; i++)
    {
        int tick_x_location =
            (float)axis_length_ / ((float)tick_num_ - 1) * (float)i + 0.5; // 四舍五入

        int minitick_width = 1;
        int minitick_height = 3;
        QRect minitick_rect(tick_x_location + offset_, axis_height, minitick_width,
                            minitick_height);
        painter->fillRect(minitick_rect, QColor("red"));

        QString tick_val;
        tick_val = QString::asprintf("%.2f", min_tick_ + (max_tick_ - min_tick_) /
                                                             ((float)tick_num_ - 1.0) * (float)i);
        QFont font;
        font.setFamily("Microsoft Yahei");
        font.setPointSize(font_size_);
        QFontMetrics fm(font);
        QRect rec = fm.boundingRect(tick_val);
        int text_width = rec.width();
        int text_height = rec.height();

        int text_minitick_dist = 3;
        QRect text_rect(tick_x_location - text_width / 2 + offset_,
                        axis_height + minitick_height + text_minitick_dist, text_width,
                        text_height);
        painter->setFont(font);
        painter->drawText(text_rect, tick_val);
    }
}
void ImageAxis::paintVerticalAxis(QPainter* painter)
{

    qDebug() << "vertical axis: height=" << height() << " width=" << width()
             << " axis_length_=" << axis_length_;

    int axis_width = 1;
    QRect ytick_rect(width() - axis_width, offset_, axis_width, axis_length_);
    painter->fillRect(ytick_rect, QColor("red"));
    for (int i = 0; i < tick_num_; i++)
    {
        int tick_y_location = (float)axis_length_ -
                              (((float)axis_length_) / ((float)tick_num_ - 1) * (float)i) +
                              0.5; // 这里axis_length_减去1是因为minitick_height为1
        int minitick_width = 3, minitick_height = 1;
        QRect minitick_rect(width() - axis_width - minitick_width, tick_y_location + offset_,
                            minitick_width, minitick_height);
        painter->fillRect(minitick_rect, QColor("red"));

        QString tick_val;
        tick_val = QString::asprintf("%.2f", min_tick_ + (max_tick_ - min_tick_) /
                                                             ((float)tick_num_ - 1.0) * (float)i);
        QFont font;
        font.setFamily("Microsoft Yahei");
        font.setPointSize(font_size_);
        QFontMetrics fm(font);
        QRect rec = fm.boundingRect(tick_val);
        int text_width = rec.width();
        int text_height = rec.height();
        // int text_width = fm.width(tick_val);
        // int text_height = fm.height(tick_val);
        // qDebug() <<"text width=" << text_width << " height=" << text_height;

        int text_minitick_dist = 5;

        QRect text_rect(width() - axis_width - minitick_width - text_minitick_dist - text_width,
                        tick_y_location - text_height / 2 + offset_, text_width, text_height);
        painter->setFont(font);
        painter->drawText(text_rect, tick_val);
    }
}
void ImageAxis::rePaint()
{
    this->update();
    // std::cout <<"axis repaint!!!" << std::endl;
}
